<?php
namespace app\mobile\controller;

use app\mobile\service\CompanyService;
use app\mobile\service\IncreaseService;
use app\mobile\service\ExportService;
use app\common\model\SystemIncrease;
use app\admin\validate\Increase as IncreaseValidate;
use app\admin\service\IncreaseService as AdmIncService;
use library\Controller;
use think\Db;

/**
 * 增员确认
 * Class Incconfig
 * @package app\mobile\controller
 */

class Incconfig extends Base
{
	/**
     * 增员确认列表
     */
    public function index()
    {
    	$where[] = ['d.company_id','eq',$this->company['id']];
		$where[] = ['d.is_deleted','eq',0];
		$status = $this->request->get('status');
        if($status){
            $where[] = ['d.status','eq',$status];
        }else{
        	$where[] = ['d.status','in',[2,3,4]];
        }
		$search =  $this->request->get('search');
		if($search){
			$where[] = ['i.code|i.name|i.id_number','like','%'.$search.'%'];
		}
        $list = Db::name('system_increase_insurance')
        		->alias('d')
				->join('system_increase i','d.increase_id = i.id')
        		->where($where)
				->field('d.id,d.confirm_date,d.status,d.insurance,i.code,i.name,i.id_number,i.mobile')
    			->order('d.id desc')
    			->limit(10)
    			->select();
		$this ->insurance = get_option('insurance');
        $this ->list = $list; 	
        $this ->total = Db::name('system_increase_insurance')->where(['company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>4])->count();	
        $this->fetch();
    }
    
    
    /**
     * 增员确认列表ajax
     */
    public function page_list()
    {
    	$page = $this->request->post('page');
    	if(!is_numeric($page)){
			$this->error('参数错误');
		}
		$minpage = ($page-1)*10;
		$where[] = ['d.company_id','eq',$this->company['id']];
		$where[] = ['d.is_deleted','eq',0];
		$status = $this->request->post('status');
        if($status){
            $where[] = ['d.status','eq',$status];
        }else{
        	$where[] = ['d.status','in',[2,3,4]];
        }
		$search =  $this->request->post('search');
		if($search){
			$where[] = ['i.code|i.name|i.id_number','like','%'.$search.'%'];
		}
        $list = Db::name('system_increase_insurance')
        		->alias('d')
				->join('system_increase i','d.increase_id = i.id')
        		->where($where)
				->field('d.id,d.confirm_date,d.status,d.insurance,i.code,i.name,i.id_number,i.mobile')
    			->order('d.id desc')
    			->limit($minpage,10)
    			->select();
    	if($list){
    		foreach($list as $k=>$v){
	    		$list[$k]['insurance'] = get_option('insurance')[$v['insurance']]; 		
	    	}
    	}
        $this->success('获取成功',$list);	
		
    }
    
    
    
    /**
     * 增员确认反馈编辑
     */
    public function edit()
    {
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		trim_array($data);
    		$increase_insurance = Db::name('system_increase_insurance')->where(['id'=>$data['id']])->find();
    		$increase = Db::name('system_increase')->where(['id'=>$increase_insurance['increase_id'],'company_id'=>$this->company['id'],'is_deleted'=>0])->find();
			if(!$increase){
				$this->error('暂无员工信息');
			}
			$person = Db::name('system_person')->where(['id'=>$increase['person_id'],'company_id'=>$this->company['id']])->find();
			if(!$person || in_array($person['status'], [2,3])){
				$this->error('该员工不在职，无法增加险种');
			}
			$find = Db::name('system_downsizing')->where(['person_id'=>$increase['person_id'],'status'=>[1,3],'is_deleted'=>0])->find();
			if($find){
				$this->error('该员工正在办理减员，无法增加险种');
			}
			$contract = Db::name('system_contract')->where(['company_id'=>$this->company['id']])->order('id desc')->field('id,status')->find();
			if($contract['status'] == 2){
				$this->error('合同已关闭，无法增加险种');
			}			
			if(in_array($increase_insurance['status'],[1,2])){
				$this->error('该险种正在办理增加，请勿重复提交');
			}
			if($increase_insurance['status'] == 3){
				$this->error('该险种已在职，无法增加险种');
			} 
			if(!after_accounting($data['account_period'],$data['start_date'])){
       			$this->error('起始日期不能超过所属账期');
       		}   		    		
			Db::name('system_increase_insurance')->where(['id'=>$data['id']])->update(['wage'=>$data['wage'],'company_base'=>$data['company_base'],'personal_base'=>$data['personal_base'],'start_date'=>$data['start_date'],'account_period'=>$data['account_period'],'status'=>2]);
			$this->success('努力提交中......', url('mobile/incconfig/addSuccess'));
    	}else{
    		$id = $this->request->get('id');
	    	$increase_insurance = Db::name('system_increase_insurance')->where(['id'=>$id])->find();
	    	if(!$increase_insurance){
				$this->redirect('mobile/publicot/transition', ['info'=>'暂无员工信息','type'=>'5']);
			}		
			$increase = Db::name('system_increase')->where(['id'=>$increase_insurance['increase_id'],'company_id'=>$this->company['id'],'is_deleted'=>0])->find();
			$this->increase_insurance = $increase_insurance;
			$this->increase = $increase;
			$this ->insurances = get_option('insurance');
			$date = date('Y-m-d',time());
			$nextmonth = after_month($date,1);
			$this->nextmonth = $nextmonth;
					
    	}
        $this->fetch();
    }
    
    
    /**
     * 增员查看
     */
    public function view()
    {
    	$id = $this->request->get('id');
    	$increase_insurance = Db::name('system_increase_insurance')->where(['id'=>$id])->find();
    	if(!$increase_insurance){
			$this->redirect('mobile/publicot/transition', ['info'=>'暂无员工信息','type'=>'5']);
		}		
		$increase = Db::name('system_increase')->where(['id'=>$increase_insurance['increase_id'],'company_id'=>$this->company['id'],'is_deleted'=>0])->find();
		$this->increase_insurance = $increase_insurance;
		$this->increase = $increase;
		$this ->insurances = get_option('insurance');
        $this->fetch();
    }
    
    
    /**
     * 增员成功
     */
    public function addSuccess()
    {
        $this->fetch();
    }
    
    
    /**
	 * 根据工资获取单一基数
	 */
	public function insuranceBase(){
		$wage = $this->request->post('wage');
		$insurance = $this->request->post('insurance');			
		if(!is_numeric($wage) || !in_array($insurance, [1,2,3,4,5])){
			$this->error('参数错误');
		}
		$data = AdmIncService::oneBase($insurance,$wage);
		$this->success('获取成功',$data);
	}
    
    
    /**
     * 导出
     */
    public function export()
    {
        $data = $this->request->param();
    	$ids = explode(',', $data['id']);
		$list = ExportService::export_incconfig($ids,$this->company['id']);
    	toexcel($list['filename'],$list['titles'],$list['list']);  
    }
    
}